let fs = require("fs");

if (!fs.existsSync("bigdata"))fs.mkdirSync("bigdata");

let  str = "azwsxedcqq";
let startAt = new Date().getTime();
let fd = fs.openSync("./bigdata/big.txt","a");

for (let i = 0; i < 100; i++) {
    fs.writeSync(fd,str.slice(Math.floor(Math.random()*(str.length-1))),str.length-1);
    
}

fs.closeSync(fd);
let endAt = new Date().getTime();
//console.log(endAt-startAt);

let chatArray = [];
let readStream = fs.createReadStream("./bigdata/big.txt");

readStream.on("data",(chunk)=>{

    let str = chunk.toString();
    for (let i = 0; i < str.length; i++) {
        if (chatArray[str[i]]==undefined) {
            chatArray[str[i]] = 1;
        } else {
            chatArray[str[i]] += 1;
        }
        
    }
});

readStream.on("end",()=>{
    console.log(chatArray);
    var max = 0;
    var z = '';
    for (const key in chatArray) {
        if(max < chatArray[key]){
            max = chatArray[key];
            z = key;
        }
    }
    console.log("出现次数最多为："+max);
    console.log("出现次数最多的字符为："+z);
})




    
